﻿using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using ProjTemp.Desktop.Common.Controls;
using System.Windows.Controls;
using Csla;
using ProjTemp.Desktop.Common;
using MahApps.Metro.Controls;
using System;
using Mysoft.ServerMonitoring.OMKit.ViewModels;

namespace Mysoft.ServerMonitoring.OMKit.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [Export]
    public partial class Shell : CustomMetroWindow
    {
        public Shell()
        {
            InitializeComponent();
        }

        [Import]
        internal ShellViewModel ViewModel
        {
            get { return this.DataContext as ShellViewModel; }
            set { this.DataContext = value; }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOverlayAsync().ContinueWith(t =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (this.Flyouts.Items.Count > 0)
                    {
                        var flyout = this.Flyouts.Items[0] as Flyout;
                        flyout.IsOpen = !flyout.IsOpen;
                        flyout.IsOpenChanged += (s, re) =>{ if(!((Flyout)s).IsOpen) this.HideOverlayAsync(); };
                    }
                }));
            });      
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var contextMenu = ((Button)sender).ContextMenu;
            contextMenu.PlacementTarget = (sender as Button);
            contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
            contextMenu.IsOpen = true;            
        }

        private void Button_PreviewMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }
}
